// b2c/address/address.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cookie: null,
    address:[],
    show:false,
    list:" "
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const self = this, a = wx.getStorageSync('cookie') || [];
    this.setData({
      cookie: a
    })
    wx.setNavigationBarTitle({
      title: '收货地址'
    })
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadAddress()
  },
  lst(res){
    const self = this;
    const address = res.currentTarget.dataset;
    // app.globalData._dzname = address.name;
    // app.globalData.addressId = `${address.prov}${address.city}${address.district}${address.address}`;
    // app.globalData._dzphone = address.phone;
    // app.globalData._dzaddress = `${address.prov}${address.city}${address.district}${address.address}`;
    // app.globalData.prov = address.prov;
    // app.globalData['nt'] = "1";
    // console.log(app.globalData['nt'])
    console.log(self)
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_add_address",
      data: {
        xgid: address.id,
        name: address.name,
        hometel: "",
        phone: address.phone,
        prov: address.prov,
        city: address.city,
        qu: address.district,
        address: address.address,
        post: address.post,
        mrdz: true,
        lng: address.lng,
        lat: address.lat
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: (res) => {
        console.log(res)
        const json = JSON.parse(res.data);
        if (json.ret == "添加成功" || "修改成功") {
          app.globalData.nt = "2"
          wx.navigateBack()
        } else if (json.error == "最多只能设置6个地址，当前已经超出！") {
          wx.navigateBack()
        }
      },
      fail: (res) => {
        console.log(res)
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },
  /*加载地址*/
  loadAddress(){
    const self = this;
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_address_show",
      data: {
        a:""
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: (res) => {
        console.log(res)
        if(res.data.length>0){
          const json = res.data;
          let arr = [];
          json.forEach(item=>{
            console.log(item)
            arr.push({
              id:item.id,
              phone: item.phone,
              name: item.name,
              prov: item.prov,
              city: item.city,
              district: item.district,
              address: item.address,
              isuse: item.isuse,
              lat:item.lat,
              lng:item.lng,
              show:false,
              post:item.post
            })
            self.setData({
              address: arr,
              show:false
            })
          })
          
        }else{
          self.data.show = true;
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  editor(res){
    const cs = res.currentTarget.dataset.cs, self = this, id = res.currentTarget.dataset.id;
    if( +cs == 1){
      wx.navigateTo({
        url: '../ad/ad',
      })
    }else if(+cs == 3){
      wx.showModal({
        title: '删除提示',
        content: '删除后将无法恢复数据?',
        success: function (res) {
          if (res.confirm) {
            wx.request({
              url: "https://csb2c.fenxiangqu.cn/xcx_deleteAddress",
              data: {
                id: id
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Cookie': "LID=" + self.data.cookie
              },
              method: "POST",
              success: (res) => {
                console.log(res)
                  if(res.data){
                    if(res.data.n == 1 && res.data.ok == 1){
                      app.globalData['nt'] = "2";
                      wx.showToast({
                        title: '删除成功',
                        icon: 'success',
                        duration: 2000
                      })
                      self.loadAddress()
                    }else{
                      wx.showToast({
                        title: '删除失败',
                        icon: 'success',
                        duration: 2000
                      })
                    }
                  }
              },
              fail: (res) => {
                wx.showToast({
                  title: '网络请求失败',
                  icon: 'success',
                  duration: 1000
                })
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }else{
      wx.navigateTo({
        url: '../bjaddress/bjaddress?id=' + id
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})